package com.niit.cms.controller;

import com.niit.cms.entity.Order;
import com.niit.cms.entity.Orderdetail;
import com.niit.cms.entity.Userinfo;
import com.niit.cms.service.CourseService;
import com.niit.cms.service.OrderService;
import com.niit.cms.service.OrderdetailService;
import com.niit.cms.service.UserinfoService;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:
 * @Author: cao_jiaqi
 * @CreateTime： 2022/7/1 19:23
 */
@Controller
public class MemberController {
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderdetailService orderdetailService;
    @Resource
    private CourseService courseService;

    /**
     * @Description: 个人主页
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/1 19:43
     * @return: java.lang.String
     **/
    @GetMapping("/member")
    public String index(){

        return "member_info";
    }
    /**
     * @Description: 初始化用户修改页面
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/2 13:36
     * @return: java.lang.String
     **/
    @GetMapping("/memberUpdate")
    public String updateIndex(){

        return "member_update";
    }
    /**
     * @Description: 初始化修改密码页面
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/2 13:37
     * @return: java.lang.String
     **/
    @GetMapping("/memberPwd")
    public String updatePwdIndex(){

        return "member_pwd";
    }
    /**
     * @Description: 修改用户信息
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/2 13:50
     * @return: java.lang.String
     **/
    @PutMapping("/memberUpdate")
    public String update(Model model, Userinfo userinfo, HttpSession session){
        Userinfo u = userinfoService.update(userinfo);
        if(u != null){
            session.setAttribute("user", u);
            return "redirect:/member";
        }
        return "member_update";
    }
    /**
     * @Description: 查询用户所有订单
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/4 10:35
     * @param: model
     * @param: session
     * @return: java.lang.String
     **/
    @GetMapping("/memberOrder")
    public String memberOrder(Model model, HttpSession session){
        Userinfo userinfo = (Userinfo) session.getAttribute("user");
        List<Order> olist = orderService.queryListByUid(userinfo.getId());
        model.addAttribute("olist", olist);
        return "member_order";
    }
    /**
     * @Description: 查询用户某订单的订单详情
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/4 11:16
     * @param: model
     * @param: oid
     * @return: java.lang.String
     **/
    @GetMapping("/memberOrderdetail")
    public String memberOrderdetail(Model model, @RequestParam("oid") Integer oid){
        //查询该订单
        Order order = orderService.queryById(oid);
        model.addAttribute("order", order);
        //查询该订单订单详情
        List<Orderdetail> orderdetailList = orderdetailService.queryListByOid(oid);
        for(Orderdetail orderdetail : orderdetailList){
            orderdetail.setCourse(courseService.queryById(orderdetail.getCourseId()));
        }
        model.addAttribute("odlist", orderdetailList);
        return "member_order_detail";
    }


}
